﻿using Ebusiness_OrderService.DomainEvent;
using Ebusiness_OrderService.Model;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;

namespace Ebusiness_OrderService.Data;

//[ConnectionStringName("OrderSetvice")]
public class Ebusiness_OrderServiceDbContext : AbpDbContext<Ebusiness_OrderServiceDbContext>
{
    public Ebusiness_OrderServiceDbContext(DbContextOptions<Ebusiness_OrderServiceDbContext> options)
        : base(options)
    {

    }

    public IMediator mediator { get; set; }

    public DbSet<Order> orders { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        /* Include modules to your migration db context */

        /* Configure your own entities here */
    }

    //public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
    //{
    //    //获取所有含有未发布事件的实体对象
    //    var domainEntitys = this.ChangeTracker.Entries<IDomainEvents>().Where(x => x.Entity.GetDmoainEvents().Any());
    //    //获取所有待发消息
    //    var domainEvents = domainEntitys.SelectMany(x => x.Entity.GetDmoainEvents()).ToList();
    //    domainEntitys.ToList().ForEach(x => x.Entity.ClearDmoainEvent());
    //    foreach (var domainEvent in domainEvents)
    //    {
    //        await mediator.Publish(domainEvent);
    //    }
    //    return await base.SaveChangesAsync(cancellationToken);
    //}
}
